package A一周刷爆LeetCode.A基础班.E链表;

/**
 * @author wei.zhao
 * @description: 两个单链表相交问题
 * @date 2022年01月27日 17:37
 */
public class Code07_FindFirstIntersectNode {

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);
    }

    //获取相交节点
    public static Node getIntersectNode(Node head1, Node head2) {
        Node loopNode1 = getLoopNode(head1);
        Node loopNode2 = getLoopNode(head2);
        if (null == loopNode1 || null == loopNode2) {
            // 两个链表都不是循环链表
            return getIntersectNodeNoLoop(head1, head2);
        } else if (null != loopNode1 && null != loopNode2) {
            //两个链表都是循环链表
            return getIntersectNodeBothLoop(head1, head2, loopNode1, loopNode2);
        }
        return null;
    }

    //找链表的入环节点
    public static Node getLoopNode(Node head) {
        //快慢指针，如果有环快慢指针一定会相遇，相遇后快指针重置到头节点，再次相遇即是入环节点
        Node slow = head.next;
        if (null == head.next) {
            return null;
        }
        Node fast = head.next.next;
        while (slow != fast) {
            slow = slow.next;
            if (null == fast.next || null == fast.next.next) {
                return null;
            }
            fast = fast.next.next;
        }
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    //都无环的链表返回相交节点
    public static Node getIntersectNodeNoLoop(Node head1, Node head2) {
        //求链表长度
        int length1 = getLength(head1);
        int length2 = getLength(head2);
        Node next1 = head1;
        Node next2 = head2;
        //较长的链表先走差值步
        if (length1 > length2) {
            int diff = length1 - length2;
            for (int i = 0; i < diff; i++) {
                next1 = next1.next;
            }
        } else {
            int diff = length2 - length1;
            for (int i = 0; i < diff; i++) {
                next2 = next2.next;
            }
        }
        //如果相等则返回
        while (next1 != next2) {
            next1 = next1.next;
            next2 = next2.next;
            if (null == next1 || null == next2) {
                return null;
            }
        }
        return next1;
    }

    //都有环的链表返回相交节点
    public static Node getIntersectNodeBothLoop(Node head1, Node head2, Node loopNode1, Node loopNode2) {
        if (loopNode1 == loopNode2) {
            //入环节点是同一个，等价于无环链表的相交问题
            int length1 = getLength(head1, loopNode1);
            int length2 = getLength(head2, loopNode2);
            Node next1 = head1;
            Node next2 = head2;
            //较长的链表先走差值步
            if (length1 > length2) {
                int diff = length1 - length2;
                for (int i = 0; i < diff; i++) {
                    next1 = next1.next;
                }
            } else {
                int diff = length2 - length1;
                for (int i = 0; i < diff; i++) {
                    next2 = next2.next;
                }
            }
            //如果相等则返回
            while (next1 != next2) {
                next1 = next1.next;
                next2 = next2.next;
                if (next1 == next2) {
                    return next1;
                }
            }
            return null;
        } else {
            //入环节点不是同一个，其中一个节点从入环节点开始走直到回到入环节点，判断各节点是否等于另一链表的入环节点，都不想等，不相交；相等，返回两链表入环节点的任意一个。
            Node next = loopNode1.next;
            while (next != loopNode1) {
                if (next == loopNode2) {
                    return loopNode2;
                }
                next = next.next;
            }
        }
        return null;
    }

    //求链表的长度
    public static int getLength(Node head) {
        Node next = head;
        int length = 0;
        while (next != null) {
            length++;
            next = next.next;
        }
        return length;
    }

    //求链表到某一节点的长度
    public static int getLength(Node head, Node node) {
        Node next = head;
        int length = 0;
        while (next != null && next != node) {
            length++;
            next = next.next;
        }
        return length;
    }

}
